using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

using BPMN.MainWindow;

namespace BPMN.MainWindow
{
    public partial class UCPoolProperties : UserControl, IUpdateOnEvent
    {
        FigureEvent eventMethod;
        FigureParentChild currentFigure= null;
        WindowMain windowMain;
        #region IUpdateOnEvent Members

        public UCPoolProperties()
        {
            InitializeComponent();
            eventMethod = new FigureEvent(update);
            RegisterForReceivingUpdates();
            textBox1.KeyUp += new KeyEventHandler(textBox1_KeyUp);
        }
        public void SetParent(WindowMain windowMain)
        {
            this.windowMain = windowMain;
        }
        void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (currentFigure != null)
                {
                    if (textBox1.Text != "")
                    {
                        windowMain.BPMNClass.Pools[currentFigure.Name].Name = textBox1.Text;
                        FigureParentChild fpc1 = new FigureParentChild(currentFigure.Name, null, FigureType.Pool);
                        FigureParentChild fpc2 = new FigureParentChild(textBox1.Text, null, FigureType.Pool);
                        EventManagement.SendEventSignal(this, new FigureUpdateEventArgs(fpc1,fpc2, FigureEventType.Modified, FigureType.Pool));
                        currentFigure=new FigureParentChild( textBox1.Text,null, FigureType.Pool);
                    }
                    else
                    {
                        MessageBox.Show("Name cannot be empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }
                }
            }
        }

        public void RegisterForReceivingUpdates()
        {
            EventManagement.GetInstance().RegisterNewModule(FigureType.Pool,new ModuleEventStruct( this, eventMethod));
        }

        public void update(object sender, FigureUpdateEventArgs args)
        {
            if (sender != null && args != null)
            {

                switch (args.Type)
                {
                    case FigureEventType.Selection:
                        currentFigure = args.NewFigure;
                        textBox1.Text = args.NewFigure.Name;
                        break;
                    case FigureEventType.Modified:

                        break;
                }


            }
        }
        #endregion
    }
}
